package org.tissou.server.Utils;


import java.sql.*;
import java.util.logging.Logger;

import org.tissou.client.Utils.TestOuComptes;

/**
 * Created by IntelliJ IDEA.
 * User: Claire
 * Date: 22-Nov-2008
 * Time: 15:45:10
 * To change this template use File | Settings | File Templates.
 */
public class InterfaceMySQL {

  private static final Logger LOG = Logger.getLogger(InterfaceMySQL.class.getName());

    Connection dbConnection;

   public InterfaceMySQL() {
        try {
          Class.forName("com.mysql.jdbc.Driver").newInstance();
          String base = "Comptes";
          if (TestOuComptes.TEST) {      
            base = "test";
          }
          dbConnection = DriverManager.getConnection("jdbc:mysql://localhost/" + base, "manu", "letsdoit");
        } catch (InstantiationException e) {
          throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
          throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
          throw new RuntimeException(e);
        } catch (SQLException e) {
          throw new RuntimeException(e);
        }

    }

    public static InterfaceMySQL getInstance() {
        return new InterfaceMySQL();
    }

    public void EnvoyerQuery(String Query) throws SQLException {
      LOG.info("Query: "+Query);
      Statement statement = dbConnection.createStatement();
      statement.executeUpdate(Query);
    }

    public ResultSet RecupererDonnees(String Query) throws SQLException {
      Statement statement = dbConnection.createStatement();
      System.out.println(Query);
      return statement.executeQuery(Query);
    }

}
